import { Inject, Controller, Get, Post, Param} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { TransferService } from '../../service/admin/transfer.service';
import { PetService } from '../../service/user/pet.service';
import * as fs from 'fs';
import * as path from 'path';


@Controller('/api/admin')
export class APIController {
  @Inject()
  ctx: Context;

  @Inject()
  transferService: TransferService;
  @Inject()
  petService: PetService;

  @Get('/getAlltransfer', { description: '获得所有转养' })
  async gettransfer() {
    const result = await this.transferService.gettransfer()
    console.log("result.length",result.length);
    if(result.length!=0){
      for(var i = 0 ;i< result.length; i++){
        const petinfo = await this.petService.petinfo(result[i].petid)
        console.log("petinfo",petinfo);
        const fileName = petinfo.petpic.imageurl;
        console.log("fileName",fileName);
        const petpic = fs.readFileSync(path.join(__dirname, '../../../public/upload', fileName));
        result[i]["src"] = petpic
        result[i]["petinfo"] = petinfo 
        result[i]["type"] = "转养"      
      } 
    }
    return { success: true, message: '转让查询成功',  result }
  }

  @Post('/deletetransfer/:id', { description: '删除转养' })
  async Param(@Param() Param) {
      const result = await this.transferService.deletetransfer(Param.id)
      return { success: true, message: '删除成功', result }
  }


}